package com.example.my_music_palyer.controller;

import com.example.my_music_palyer.model.ImageAndName;
import com.example.my_music_palyer.model.User;
import com.example.my_music_palyer.service.ImageService;
import com.example.my_music_palyer.common.tools.ImageTools;
import com.example.my_music_palyer.common.tools.ResponseBodyMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/image")
public class ImageController {

    @Value("${image.pre}")
    String pre;
    @Autowired
    ImageTools imageTools;

    @Autowired
    ImageService imageService;


    @RequestMapping("/upload")
    ResponseBodyMessage<Integer> upload(@RequestParam("file") MultipartFile file, HttpServletResponse resp
                                        , HttpServletRequest req) throws IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            return new ResponseBodyMessage<>(-1, "上传失败，用户未登录", 0);

        }
        //通过session获取用户信息
        User user = (User) session.getAttribute("user");
        if (session == null)
            if (user == null || user.getId() < 0) {//用户不存在或者Session中数据不存在
                return new ResponseBodyMessage<>(-1, "上传失败，用户不存在", 0);
            }

        //用户存在，本地上传图像
        int result = imageTools.upload(file, user.getId());


        if (result == 1) {//本地上传成功

            String fileName = file.getOriginalFilename();//获取文件名


            //数据库上传
            //1.将原来使用的图片flg设置为0
            // A.获取用户当前使用的图片URL
            String preUrl = imageService.getUseImageUrl(user);
            log.info(preUrl.toString());
            // B.将当前使用的图片flg设置为0
            imageService.updateImage(user, 0, preUrl);

            //2.数据库中存储图片
            int ret = imageService.uploadImage(fileName, user);

            //3.判断数据库是否 存储成功
            if (ret != 1) {
                //数据库存储失败
                // 还原当前使用图片
                imageService.updateImage(user, 1, preUrl);
                //同时删除本地存储文件
                int localret = imageTools.delete(fileName);
                if (localret == 1) {
                    log.info("本地删除成功");
                } else {
                    log.info("本地删除失败!!!");
                }
                return new ResponseBodyMessage<>(-1, "上传失败，联系管理员", 0);

            }

            resp.sendRedirect("/list.html");
            return new ResponseBodyMessage<>(0, "上传成功", 1);

        }
        return new ResponseBodyMessage<>(-1, "上传失败，联系管理员", 0);

    }

   /* 测试
   @RequestMapping("/upload")
    ResponseBodyMessage<Integer> upload(@RequestParam("file") MultipartFile file,int id) throws IOException {

        //用户存在，上传图像
        int result = imageUpload.upload(file,id);
        if(result==1){//上传成功
            return new ResponseBodyMessage<>(0,"上传成功",1);

        }
        return new ResponseBodyMessage<>(-1,"上传失败，联系管理员",0);

    }*/


    @RequestMapping("/getImage")
    ImageAndName getImage(HttpServletRequest req) {

        HttpSession session = req.getSession(false);

        //通过session获取用户信息
        User user = (User) session.getAttribute("user");
        String ret =  imageService.getUseImageUrl(user);
        ImageAndName imageAndName = new ImageAndName();
        imageAndName.setUrl(ret);
        imageAndName.setUsername(user.getUsername());

        imageAndName.setPreurl(pre);
        return imageAndName;
    }
}
